home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
fe1.zip
/
BMP.CPP
next >
Wrap
C/C++ Source or Header
|
1992-02-07
|
18KB
|
700 lines
#include <math.h>
#include <string.h>
#include <stdio.h>
#include "bmp.h"
const char editmap :: mode_s = 'S' ;
const char editmap :: mode_x = 'X' ;
const char *editmap :: mode_str = "MODE:%c" ;
editmap :: editmap ( int wd, int hg, char *fn ) :
win( 0, 0, fg.displaybox[FG_X2], fg.displaybox[FG_Y2], FG_WHITE,
FG_BLACK, NULL, 19, 2 ), w( wd ), h( hg ), draw_func( draw_pixel ),
filename( fn ), clipb( NULL )
{
#define MOSTLENGTH 13
#define MOSTLENGTHP1 14
#define MOSTLENGTHP2 15
int i ;
fg_coord_t k ;
bits = alloc_array( w, h ) ;
mode = new char [MOSTLENGTH] ;
func = new char [MOSTLENGTH] ;
sprintf( mode, mode_str, mode_s ) ;
for ( i = 0 ; i < w ; i++ )
memset( bits[i], 0, h ) ;
cellbox[FG_X1] = cellbox[FG_Y1] = 0 ;
cellbox[FG_X2] =
( fg.displaybox[FG_X2] -
fg.charbox[FG_X2] * MOSTLENGTHP2 ) / w ;
cellbox[FG_Y2] = ( fg.displaybox[FG_Y2] - fg.charbox[FG_Y2] * 2 ) / h ;
fg_make_box( pix, -1, -1, -1, -1 ) ;
fg_make_box( editbox, 0, 0, cellbox[FG_X2] * w, cellbox[FG_Y2] * h );
editbox[FG_X1] += fg.charbox[FG_X2] ;
editbox[FG_X2] += fg.charbox[FG_X2] ;
editbox[FG_Y1] += fg.charbox[FG_Y2] ;
editbox[FG_Y2] += fg.charbox[FG_Y2] ;
kbd->add_script( 'q', destroy ) ;
kbd->add_script( 'l', _set_draw_line ) ;
kbd->add_script( 'p', _set_draw_pixel ) ;
kbd->add_script( 'r', _redraw ) ;
kbd->add_script( 'f', _set_floodfill ) ;
kbd->add_script( 'c', _set_draw_circle ) ;
kbd->add_script( 'e', _set_draw_ellipse ) ;
kbd->add_script( 'b', _set_draw_box ) ;
kbd->add_script( 'm', set_mode ) ;
kbd->add_script( 'i', _set_invert_box ) ;
kbd->add_script( 'a', _set_clear ) ;
kbd->add_script( 'y', _set_copy ) ;
kbd->add_script( 'u', _set_cut ) ;
kbd->add_script( 'z', _set_paste ) ;
kbd->add_script( 'x', byebye ) ;
kbd->add_script( 's', save ) ;
kbd->add_script( ',', _set_flip_x ) ;
kbd->add_script( '.', _set_flip_y ) ;
kbd->add_script( '\x1b', finish_func ) ;
box->add_script( editbox, edit_map ) ;
box->add_script( winbox, total_map ) ;
k = fg.displaybox[FG_X2] - fg.charbox[FG_X2] * MOSTLENGTH ;
fg_make_box( dispbox, k, fg.charbox[FG_Y2], k + w - 1,
fg.charbox[FG_Y2] + h - 1 ) ;
#define ITEMS 24
mu = new menu ( k, fg.displaybox[FG_Y2] - fg.charbox[FG_Y2] * ITEMS,
MOSTLENGTH - 2, ITEMS, this ) ;
strcpy( func, "PIXEL" ) ;
mu->add_item( '\x0', "[P]ixel", _set_draw_pixel ) ;
mu->add_item( '\x0', "[L]ine", _set_draw_line ) ;
mu->add_item( '\x0', "[B]ox", _set_draw_box ) ;
mu->add_item( '\x0', "[C]ircle", _set_draw_circle ) ;
mu->add_item( '\x0', "[E]llipse", _set_draw_ellipse ) ;
mu->add_item( '\x0', "[F]ill", _set_floodfill ) ;
mu->add_item( '\x0', "[I]nvert", _set_invert_box ) ;
mu->add_item( '\x0', "--------", NULL ) ;
mu->add_item( '\x0', "Cop[Y]", _set_copy ) ;
mu->add_item( '\x0', "C[U]t", _set_cut ) ;
mu->add_item( '\x0', "Paste[Z]", _set_paste ) ;
mu->add_item( '\x0', "Cle[A]r", _set_clear ) ;
mu->add_item( '\x0', "--------", NULL ) ;
mu->add_item( '\x0', "[M]ode", set_mode ) ;
mu->add_item( '\x0', "[R]edraw", _redraw ) ;
mu->add_item( '\x0', "FlipX[,]", _set_flip_x ) ;
mu->add_item( '\x0', "FlipY[.]", _set_flip_y ) ;
mu->add_item( '\x0', "--------", NULL ) ;
mu->add_item( '\x0', "[S]ave", save ) ;
mu->add_item( '\x0', "[Q]uit", destroy ) ;
mu->add_item( '\x0', "e[X]it", byebye ) ;
mu->add_item( '\x0', "--------", NULL ) ;
mu->add_item( '\x0', func, NULL ) ;
mu->add_item( '\x0', mode, NULL ) ;
}
void editmap :: delete_array ( char **a, int w ) {
if ( a ) {
for ( int i = 0 ; i < w ; i++ )
delete a[i] ;
delete a ;
}
}
char **editmap :: alloc_array ( int w, int h ) {
char **x ;
x = new char * [w] ;
for ( int i = 0 ; i < w ; i++ )
x[i] = new char [h] ;
return x ;
}
void editmap :: realloc_array ( char ***x, int w, int h ) {
if ( x )
delete_array( *x, w ) ;
*x = alloc_array( w, h ) ;
}
editmap :: ~editmap () {
int i ;
delete_array( bits, w ) ;
delete_array( clipb, clip_w ) ;
delete mode ;
}
void editmap :: byebye () { save() ; destroy() ; }
void editmap :: copymap ( char **b ) {
int i, j ;
for ( i = 0 ; i < w ; i++ )
for ( j = 0 ; j < h ; j++ )
bits[i][j] = b[i][j] ;
}
void editmap :: save () {
FILE *f ;
char *xn, *da ;
char *defna = "Untitled" ;
fg_box_t t ;
int i, j, k, m, kda, msk ;
if ( ( f = fopen( xn = filename, "wt" ) ) == NULL )
if ( ( f = fopen( xn = defna, "wt" ) ) == NULL )
return ;
da = new char [w + 1] ;
fg_make_box( t, 0, 0, w - 1, h - 1 ) ;
fprintf( f, "fg_box_t %s_box = { 0, 0, %d, %d } ;\n", xn, w-1, h-1 ) ;
fprintf( f, "char %s_matrix[%u] = {\n", xn, fg_matrix_size( t ) ) ;
for ( i = 0 ; i < h ; i++ ) {
fprintf( f, "\t" ) ;
for ( j = 0 ; j < w ; j++ )
da[j] = bits[j][h - i - 1] ? '#' : ' ' ;
da[j] = 0 ;
for ( k = 0 ; k < w ; k += 8 ) {
msk = 0x80 ;
for ( kda = 0, m = k ; m < k + 8 && m < w ; m++ ) {
if ( bits[m][h - i - 1] )
kda |= msk ;
msk >>= 1 ;
}
fprintf(
f, "0x%02x%c ",
kda, k + 8 < w || i + 1 < h ? ',' : ' ' ) ;
}
fprintf( f,"\t/* %s */\n", da ) ;
}
fprintf( f, "} ;\n" ) ;
fclose( f ) ;
delete da ;
}
fg_color_t editmap :: get_color () {
return p_status == MSM_BUTTONL ? fgc : bgc ;
}
int editmap :: get_mode () {
return mode[strlen(mode)-1] == mode_s ? FG_MODE_SET : FG_MODE_XOR ;
}
void editmap :: set_mode () {
if ( get_mode() == FG_MODE_SET )
sprintf( mode, mode_str, mode_x ) ;
else
sprintf( mode, mode_str, mode_s ) ;
mu->expose() ;
}
void editmap :: getmspos () {
p_status = msm_getstatus( &p_x, &p_y ) ;
p_y = fg.displaybox[FG_Y2] + 1 - p_y ;
}
void editmap :: translate () {
p_x = ( p_x - editbox[FG_X1] ) / cellbox[FG_X2] ;
p_y = ( p_y - editbox[FG_Y1] ) / cellbox[FG_Y2] ;
}
void editmap :: set_funcname ( eventscript_t e, char *n ) {
draw_func = e ;
finish_func() ;
strcpy( func, n ) ;
mu->expose() ;
}
void editmap :: _set_floodfill () { set_funcname( floodfill, "FILL" ) ; }
void editmap :: floodfill () {
if ( pix[FG_X1] == -1 ) {
msm_hidecursor() ;
fg_fill( dispbox[FG_X1] + p_x, dispbox[FG_Y1] + p_y,
get_color(), get_color() ) ;
msm_showcursor() ;
redraw( 0, 0, w - 1, h - 1 ) ;
finish_func() ;
}
}
void editmap :: _set_draw_line () { set_funcname( draw_line, "LINE" ) ; }
void editmap :: draw_line () {
fg_coord_t x1, y1, x2, y2 ;
if ( pix[FG_X1] == -1 ) {
pix[FG_X1] = p_x ;
pix[FG_Y1] = p_y ;
} else if ( pix[FG_X2] == -1 ) {
x1 = min( p_x, pix[FG_X1] ) ;
x2 = max( p_x, pix[FG_X1] ) ;
y1 = min( p_y, pix[FG_Y1] ) ;
y2 = max( p_y, pix[FG_Y1] ) ;
pix[FG_X2] = p_x ;
pix[FG_Y2] = p_y ;
pix[FG_X1] += dispbox[FG_X1] ;
pix[FG_X2] += dispbox[FG_X1] ;
pix[FG_Y1] += dispbox[FG_Y1] ;
pix[FG_Y2] += dispbox[FG_Y1] ;
msm_hidecursor() ;
fg_drawlineclip( get_color(), get_mode(), ~0, FG_LINE_SOLID,
pix, dispbox ) ;
msm_showcursor() ;
finish_func() ;
redraw( x1, y1, x2, y2 ) ;
}
}
void editmap :: _set_draw_box () { set_funcname( draw_box, "BOX" ) ; }
void editmap :: draw_box () {
fg_coord_t x1, y1, x2, y2 ;
if ( pix[FG_X1] == -1 ) {
pix[FG_X1] = p_x ;
pix[FG_Y1] = p_y ;
} else if ( pix[FG_X2] == -1 ) {
x1 = min( p_x, pix[FG_X1] ) ;
x2 = max( p_x, pix[FG_X1] ) ;
y1 = min( p_y, pix[FG_Y1] ) ;
y2 = max( p_y, pix[FG_Y1] ) ;
pix[FG_X1] = dispbox[FG_X1] + x1 ;
pix[FG_X2] = dispbox[FG_X1] + x2 ;
pix[FG_Y1] = dispbox[FG_Y1] + y1 ;
pix[FG_Y2] = dispbox[FG_Y1] + y2 ;
msm_hidecursor() ;
fg_drawbox( get_color(), get_mode(), ~0, FG_LINE_SOLID,
pix, dispbox ) ;
msm_showcursor() ;
finish_func() ;
redraw( x1, y1, x2, y2 ) ;
}
}
void editmap :: _set_invert_box () { set_funcname( invert_box, "INVERT" ) ; }
void editmap :: invert_box () {
fg_coord_t x1, y1, x2, y2 ;
if ( pix[FG_X1] == -1 ) {
pix[FG_X1] = p_x ;
pix[FG_Y1] = p_y ;
} else if ( pix[FG_X2] == -1 ) {
x1 = min( p_x, pix[FG_X1] ) ;
x2 = max( p_x, pix[FG_X1] ) ;
y1 = min( p_y, pix[FG_